﻿#include "productnetworkaccessmanager.h"

const static QString PRODUCT_BASE_URL = BASE_URL+"product/";
const static QString PRODUCT_ADD_PRODUCT_URL = PRODUCT_BASE_URL+"addProduct";
const static QString PRODUCT_UPDATE_PRODUCT_URL = PRODUCT_BASE_URL+"updateProduct";
const static QString PRODUCT_GET_PRODUCTS_BY_COMPANYID = PRODUCT_BASE_URL+"getProductsByCompanyId";
const static QString PRODUCT_UPLOAD_PRODUCT_URL = PRODUCT_BASE_URL+"uploadProduct";
const static QString PRODUCT_UPLOAD_PRODUCT_FINISHED_URL = PRODUCT_BASE_URL+"uploadProductFinished";
const static QString PRODUCT_ADD_FORMULA_URL = PRODUCT_BASE_URL+"addFormula";
const static QString PRODUCT_DELETE_FORMULA_URL = PRODUCT_BASE_URL+"deleteFormula";
const static QString RPODUCT_GET_FORMULAS_BY_COMPANYID = PRODUCT_BASE_URL+"getFormulasByCompanyId";
const static QString PRODUCT_COMBINE_PRODUCT_URL = PRODUCT_BASE_URL+"combineProduct";
const static QString PRODUCT_SPLIT_PRODUCT_URL = PRODUCT_BASE_URL+"splitProduct";
const static QString PRODUCT_COUNT_PRODUCT_URL = PRODUCT_BASE_URL+"countProduct";
const static QString PRODUCT_SANBAO_MOVE_URL = PRODUCT_BASE_URL+"sanbaoMove";
const static QString PRODUCT_UPDATE_HISTORY_PRODUCT = PRODUCT_BASE_URL+"updateHistoryProduct";
const static QString PRODUCT_GETALL_HISTORY_PRODUCTS = PRODUCT_BASE_URL+"getAllHistoryProdycts";
const static QString PRODUCT_DELETE_PRODUCT = PRODUCT_BASE_URL+"deleteProduct";
const static QString PRODUCT_REFRESH_PRODUCTS = PRODUCT_BASE_URL+"refreshProducts";
const static QString PRODUCT_GET_FORMULA_INFO = PRODUCT_BASE_URL+"getFormulaInfo";
ProductNetworkAccessManager::ProductNetworkAccessManager(QObject *parent) : BaseNetworkAccessManager(parent)
{

}

void ProductNetworkAccessManager::addProduct(
        QString companyId,
        QString type,
        QString name,
        QString unit,
        QString productNum,
        QString upperLimit,
        QString lowerLimit,
        QString brand,
        QString originPlace,
        QString specifications,
        QString barCode,
        QString remark,
        QString guidePrice1,
        QString guidePrice2,
        QString primaryDirectory,
        QString secondaryDirectory){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("type="+type+"&");
    data.append("name="+name+"&");
    data.append("unit="+unit+"&");
    data.append("productNum="+productNum+"&");
    data.append("upperLimit="+upperLimit+"&");
    data.append("lowerLimit="+lowerLimit+"&");
    data.append("brand="+brand+"&");
    data.append("originPlace="+originPlace+"&");
    data.append("specifications="+specifications+"&");
    data.append("barCode="+barCode+"&");
    data.append("remark="+remark+"&");
    data.append("guidePrice1="+guidePrice1+"&");
    data.append("guidePrice2="+guidePrice2+"&");
    data.append("primaryDirectory="+primaryDirectory+"&");
    data.append("secondaryDirectory="+secondaryDirectory+"&");

    doPost(PRODUCT_ADD_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::updateProduct(
        QString companyId,
        QString productId,
        QString type,
        QString name,
        QString unit,
        QString productNum,
        QString upperLimit,
        QString lowerLimit,
        QString brand,
        QString originPlace,
        QString specifications,
        QString barCode,
        QString remark,
        QString guidePrice1,
        QString guidePrice2,
        QString primaryDirectory,
        QString secondaryDirectory)
{
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("productId="+productId+"&");
    data.append("type="+type+"&");
    data.append("name="+name+"&");
    data.append("unit="+unit+"&");
    data.append("productNum="+productNum+"&");
    data.append("upperLimit="+upperLimit+"&");
    data.append("lowerLimit="+lowerLimit+"&");
    data.append("brand="+brand+"&");
    data.append("originPlace="+originPlace+"&");
    data.append("specifications="+specifications+"&");
    data.append("barCode="+barCode+"&");
    data.append("remark="+remark+"&");
    data.append("guidePrice1="+guidePrice1+"&");
    data.append("guidePrice2="+guidePrice2+"&");
    data.append("primaryDirectory="+primaryDirectory+"&");
    data.append("secondaryDirectory="+secondaryDirectory+"&");

    doPost(PRODUCT_UPDATE_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::refreshProducts(QString companyId, QString productIds){
    QByteArray data;

    data.append("companyId="+companyId+"&");
    data.append("productIds="+productIds);

    doPost(PRODUCT_REFRESH_PRODUCTS, data);
}

void ProductNetworkAccessManager::getProductsByCompanyId(QString companyId)
{
    QByteArray data;
    data.append("companyId="+companyId);

    doPost(PRODUCT_GET_PRODUCTS_BY_COMPANYID, data);
}

void ProductNetworkAccessManager::uploadProduct(
        QString companyId,
        QList<Product*> products)
{
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("products="+Product::toJson(products));

    doPost(PRODUCT_UPLOAD_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::uploadProductFinished(
        QString username,
        double totalMoney){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("totalMoney="+QString("%1").arg(totalMoney));

    doPost(PRODUCT_UPLOAD_PRODUCT_FINISHED_URL, data);
}

void ProductNetworkAccessManager::addFormula(QString companyId, FormulaBase &formulaBase){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("combineId="+formulaBase.combineId+"&");
    data.append("combineCount="+QString("%1").arg(formulaBase.combineCount)+"&");
    data.append("splitIds="+formulaBase.splitIds+"&");
    data.append("splitCounts="+formulaBase.splitCounts);

    doPost(PRODUCT_ADD_FORMULA_URL, data);
}

void ProductNetworkAccessManager::deleteFormula(QString companyId, QString combineId){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("combineId="+combineId);

    doPost(PRODUCT_DELETE_FORMULA_URL, data);
}

void ProductNetworkAccessManager::getFormulasByCompanyId(QString companyId){
    QByteArray data;
    data.append("companyId="+companyId);

    doPost(RPODUCT_GET_FORMULAS_BY_COMPANYID, data);
}

void ProductNetworkAccessManager::combineProduct(QString companyId, QString combineId, int combineCount){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("combineId="+combineId+"&");
    data.append("combineCount="+QString("%1").arg(combineCount));

    doPost(PRODUCT_COMBINE_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::splitProduct(
        QString username,
        QString companyId,
        QString combineId,
        int splitCount,
        QString splitInPrices){
    QByteArray data;

    data.append("username="+username+"&");
    data.append("companyId="+companyId+"&");
    data.append("combineId="+combineId+"&");
    data.append("splitCount="+QString("%1").arg(splitCount)+"&");
    data.append("splitInPrices="+splitInPrices);

    doPost(PRODUCT_SPLIT_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::countProducts(QString username, QString companyId, QList<Product*> products){
    QByteArray data;

    data.append("username="+username+"&");
    data.append("companyId="+companyId+"&");
    data.append("products="+Product::toJson(products));

    doPost(PRODUCT_COUNT_PRODUCT_URL, data);
}

void ProductNetworkAccessManager::sanbaoMove(
        QString companyId,
        QList<Product*> products,
        Product::SanBaoMoveType moveType,
        QString moveCount){
    QByteArray data;
    data.append("companyId="+companyId+"&");
    data.append("products="+Product::toJson(products)+"&");
    data.append("moveType="+QString("%1").arg(moveType)+"&");
    data.append("moveCount="+moveCount);

    doPost(PRODUCT_SANBAO_MOVE_URL, data);
}

void ProductNetworkAccessManager::updateHistoryProducts(QList<HistoryProduct*> historyProducts, int type){
    QByteArray data;

    data.append("historyProducts="+HistoryProduct::toJson(historyProducts)+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(PRODUCT_UPDATE_HISTORY_PRODUCT, data);
}

void ProductNetworkAccessManager::getAllHistoryProdycts(
        QString sourceCompanyId,
        QString targetCompanyId,
        QString productIds,
        int type){
    QByteArray data;

    data.append("sourceCompanyId="+sourceCompanyId+"&");
    data.append("targetCompanyId="+targetCompanyId+"&");
    data.append("productIds="+productIds+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(PRODUCT_GETALL_HISTORY_PRODUCTS, data);
}

void ProductNetworkAccessManager::deleteProduct(QString companyId, QString productId){
    QByteArray data;

    data.append("companyId="+companyId+"&");
    data.append("productId="+productId);

    doPost(PRODUCT_DELETE_PRODUCT, data);
}

/**
 * @brief getFormulaInfo 通过企业Id和总成商品Id获取公式信息
 * @param companyId
 * @param combineId
 */
void ProductNetworkAccessManager::getFormulaInfo(QString companyId, QString combineId){
    QByteArray data;

    data.append("companyId="+companyId+"&");
    data.append("combineId="+combineId);

    doPost(PRODUCT_GET_FORMULA_INFO, data);
}
